/**
 * 
 */
package controllers;

import helpers.ObjectCount;
import helpers.ObjectCounter;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import daos.ClassCourseDAO;
import daos.ClassCourseDAOImpl;
import daos.StudentClassDAO;
import daos.StudentClassDAOImpl;
import model.ClassCourse;
import model.Course;
import model.StudentClass;
import model.StudentProfile;
import model.TimeSlot;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author JK
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class CreateProgramController {
	/** 
	 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param classID
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	private ApplicationContext applicationContext;
	//To Do find a place to put constant
	private int CLASSCOURSESNUMBER=2;
	private StudentClassDAO	sClassDAO;
	private 	ClassCourseDAO classCourseDAO;
	//end to do
	public StudentClass createProgram(Integer classID) {
		// begin-user-code
		applicationContext = new ClassPathXmlApplicationContext(new String[] {"classpath:persistenceTest/applicationContext.xml"});
		
			sClassDAO = (StudentClassDAO)applicationContext.getBean("studentClassDAO");
				classCourseDAO = (ClassCourseDAO)applicationContext.getBean("classCourseDAO");
			
		StudentClass selectedClass= sClassDAO.findStudentClassByID(classID);
		
		for(int i=0;i<CLASSCOURSESNUMBER;i++)
		{
		createSubjectByPreferences(selectedClass,i);
		}
		sClassDAO.save(selectedClass);
		return selectedClass;
		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void createSubjectByPreferences(StudentClass selectedClass,int popularity ) {
		// begin-user-code
		List<StudentProfile>studentlist=selectedClass.getStudents();
		
		ClassCourse newClassCourse =new ClassCourse();
		Course course=findCoursebyPopularity(studentlist,popularity);
		newClassCourse.setCourse(course);
		TimeSlot timeSlot=findTimeSlotByPoPularity(studentlist,popularity);
	assignTimeSlotandSubject(newClassCourse,timeSlot);
	//classCourseDAO.findAllClassCoursesByClass(selectedClass)).add(newClassCourse);
	selectedClass.getClassCourses().add(newClassCourse);
		// end-user-code
	}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param classCourse
	 * @param timeSlot
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void assignTimeSlotandSubject(ClassCourse classCourse,
			TimeSlot timeSlot) {
		// begin-user-code
		List<TimeSlot> currentCourseHoursList=new ArrayList<TimeSlot>();
		currentCourseHoursList.add(timeSlot);
		classCourse.setConductingHours(currentCourseHoursList);
		classCourseDAO.save(classCourse);
		// end-user-code
	}
	
	//not included in the initial Design
	private TimeSlot findTimeSlotByPoPularity(List<StudentProfile>studentlist,int selectedPopularity){
		
		
		ObjectCounter oCounter=new ObjectCounter();
		for (StudentProfile studentProfile : studentlist) {
			for(TimeSlot timeslot:studentProfile.getDesiredHours()){
				oCounter.add(timeslot);
			}
		}
		return (TimeSlot) oCounter.getObjectbyDecendingOrder(selectedPopularity);
		
		
	}
	
	//not included in the initial Design
private Course findCoursebyPopularity(List<StudentProfile>studentlist,int selectedPopularity){
		
	ObjectCounter oCounter=new ObjectCounter();
	for (StudentProfile studentProfile : studentlist) {
		for(Course course:studentProfile.getDesiredCourses()){
			oCounter.add(course);
		}
	}
	return (Course) oCounter.getObjectbyDecendingOrder(selectedPopularity);
	}
}